import 'dart:ui';

import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/http/global.dart';
import 'package:tender_flutter/init/default_app.dart';
import 'package:tender_flutter/page/home/model/user_info_entity.dart';
import 'package:tender_flutter/page/login/api/api_login.dart';
import 'package:tender_flutter/providers/provider.dart';
import 'package:tender_flutter/providers/user_profile.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/router/router_map.gr.dart';
import 'package:tender_flutter/utils/jpush_verify.dart';
import 'package:tender_flutter/utils/sputils.dart';
import 'package:tender_flutter/utils/toast.dart';

class LoginManager {
  static bool islogining = false;

  /// 用户登录操作
  static Future<bool> login() async {
    if (islogining) {
      return true;
    }

    islogining = true;

    /// 执行一键登录操作
    ToastUtils.showLoading("");
    Map<dynamic, dynamic> result = await JVerifyUtils.loginAuth();
    int code = result['code'];
    String content = result['message'];
    String operator = result['operator'];
    islogining = false;
    if (code == 6000) {
      /*认证成功之后，执行登录授权接口*/
      LoginManager.quickLogin(loginToken: content);
      return true;
    } else if (code == 6002) {
      /*用户取消获取loginToken*/
      ToastUtils.dismissLoading();
      return false;
    } else {
      ToastUtils.dismissLoading();
      LRouter.navigator.push(Routes.phoneLoginPage);
      return false;
    }
  }

  /// 一键登录
  static Future<void> quickLogin({String loginToken}) async {
    if (islogining) {
      return true;
    }
    islogining = true;
    ApiResponse<UserInfoEntity> response =
        await ApiLogin.userLogin<UserInfoEntity>(authToken: loginToken);
    islogining = false;
    if (response.status == Status.SUCCESS) {
      UserInfoEntity userInfoEntity = response.result;
      loginSuccess(userInfoEntity: userInfoEntity);
    } else {
      loginFail();
    }
  }

  /// 手机号码登录
  static Future<void> phoneLogin(
      {String phone,
      String code,
      VoidCallback successCallback,
      VoidCallback failCallback}) async {
    islogining = true;
    ApiResponse<UserInfoEntity> response =
        await ApiLogin.phoneLogin<UserInfoEntity>(phone: phone, code: code);
    islogining = false;
    if (response.status == Status.SUCCESS) {
      UserInfoEntity userInfoEntity = response.result;
      loginSuccess(userInfoEntity: userInfoEntity);
      if (successCallback != null) {
        successCallback();
      }
    } else {
      loginFail();
      if (failCallback != null) {
        failCallback();
      }
    }
  }

  static void loginSuccess({UserInfoEntity userInfoEntity}) {
    /// 保存用户信息
    SPUtils.saveUserInfo(userInfoEntity);
    SPUtils.saveToken(userInfoEntity.token);

    /// 配置HTTP请求的全局token
    Global.accessToken = userInfoEntity.token;
    Store.value<UserProfileInfo>(MyApp.navigatorState.currentContext)
        .setUserInfo(userInfoEntity);
    ToastUtils.dismissLoading();
  }

  static void loginFail() {}
}
